{% extends 'mobile_app/base.html' %}
{% load static %}

{% block title %}个人资料 - 郑州移不动{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2 class="mb-0">
                <i class="fas fa-user text-primary me-2"></i>个人资料
            </h2>
        </div>
    </div>
</div>

<div class="row justify-content-center">
    <div class="col-md-8 col-lg-6">
        <!-- 头像和基本信息 -->
        <div class="card border-0 shadow-sm mb-4">
            <div class="card-body text-center">
                <div class="mb-4">
                    {% if user_profile.avatar %}
                        <img src="{{ user_profile.avatar.url }}" alt="头像" class="avatar-lg rounded-circle">
                    {% else %}
                        <div class="avatar-lg rounded-circle bg-primary text-white d-inline-flex align-items-center justify-content-center">
                            <i class="fas fa-user" style="font-size: 2rem;"></i>
                        </div>
                    {% endif %}
                </div>
                <h4 class="mb-1">{{ user.username }}</h4>
                <p class="text-muted mb-0">{{ user.email }}</p>
                <small class="text-muted">
                    注册时间：{{ user.date_joined|date:"Y年m月d日" }}
                </small>
            </div>
        </div>

        <!-- 资料编辑表单 -->
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-primary text-white">
                <h5 class="mb-0">
                    <i class="fas fa-edit me-2"></i>编辑资料
                </h5>
            </div>
            <div class="card-body">
                <form method="post" enctype="multipart/form-data">
                    {% csrf_token %}
                    
                    <div class="mb-4">
                        <label for="{{ profile_form.avatar.id_for_label }}" class="form-label">头像</label>
                        <div class="d-flex align-items-center">
                            <div class="me-3">
                                {% if user_profile.avatar %}
                                    <img src="{{ user_profile.avatar.url }}" alt="当前头像" class="avatar rounded-circle">
                                {% else %}
                                    <div class="avatar rounded-circle bg-secondary text-white d-inline-flex align-items-center justify-content-center">
                                        <i class="fas fa-user"></i>
                                    </div>
                                {% endif %}
                            </div>
                            <div class="flex-grow-1">
                                {{ profile_form.avatar }}
                                <div class="form-text">支持JPG、PNG格式，建议尺寸200x200像素</div>
                            </div>
                        </div>
                        {% if profile_form.avatar.errors %}
                            <div class="text-danger small mt-1">
                                {% for error in profile_form.avatar.errors %}
                                    <div>{{ error }}</div>
                                {% endfor %}
                            </div>
                        {% endif %}
                    </div>

                    <div class="mb-4">
                        <label class="form-label">用户名</label>
                        <input type="text" class="form-control" value="{{ user.username }}" readonly>
                        <div class="form-text">用户名不可修改</div>
                    </div>

                    <div class="mb-4">
                        <label class="form-label">邮箱地址</label>
                        <input type="email" class="form-control" value="{{ user.email }}" readonly>
                        <div class="form-text">邮箱地址不可修改</div>
                    </div>

                    <div class="mb-4">
                        <label class="form-label">姓名</label>
                        <div class="row">
                            <div class="col-6">
                                <input type="text" class="form-control" value="{{ user.first_name }}" readonly>
                                <div class="form-text">姓</div>
                            </div>
                            <div class="col-6">
                                <input type="text" class="form-control" value="{{ user.last_name }}" readonly>
                                <div class="form-text">名</div>
                            </div>
                        </div>
                    </div>

                    <div class="d-grid">
                        <button type="submit" class="btn btn-primary btn-lg">
                            <i class="fas fa-save me-2"></i>保存修改
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <!-- 账户统计 -->
        <div class="card border-0 shadow-sm mt-4">
            <div class="card-header bg-light">
                <h6 class="mb-0">
                    <i class="fas fa-chart-bar me-2"></i>账户统计
                </h6>
            </div>
            <div class="card-body">
                <div class="row text-center">
                    <div class="col-4">
                        <h5 class="text-primary">{{ user.phone_numbers.count }}</h5>
                        <small class="text-muted">手机号码</small>
                    </div>
                    <div class="col-4">
                        <h5 class="text-success">
                            {% for phone in user.phone_numbers.all %}
                                {% if forloop.first %}
                                    ¥{{ phone.balance.amount|floatformat:2 }}
                                {% endif %}
                            {% empty %}
                                ¥0.00
                            {% endfor %}
                        </h5>
                        <small class="text-muted">总余额</small>
                    </div>
                    <div class="col-4">
                        <h5 class="text-info">{{ user_profile.created_at|timesince }}</h5>
                        <small class="text-muted">注册时长</small>
                    </div>
                </div>
            </div>
        </div>

        <!-- 安全设置 -->
        <div class="card border-0 shadow-sm mt-4">
            <div class="card-header bg-warning text-dark">
                <h6 class="mb-0">
                    <i class="fas fa-shield-alt me-2"></i>安全设置
                </h6>
            </div>
            <div class="card-body">
                <div class="row align-items-center">
                    <div class="col-8">
                        <h6 class="mb-1">修改密码</h6>
                        <small class="text-muted">定期修改密码可以提高账户安全性</small>
                    </div>
                    <div class="col-4 text-end">
                        <button class="btn btn-outline-warning btn-sm" data-bs-toggle="modal" data-bs-target="#changePasswordModal">
                            <i class="fas fa-key me-1"></i>修改
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <!-- 返回按钮 -->
        <div class="text-center mt-4">
            <a href="{% url 'mobile_app:dashboard' %}" class="btn btn-outline-secondary">
                <i class="fas fa-arrow-left me-2"></i>返回仪表板
            </a>
        </div>
    </div>
</div>

<!-- 修改密码模态框 -->
<div class="modal fade" id="changePasswordModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">
                    <i class="fas fa-key me-2"></i>修改密码
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="alert alert-info">
                    <i class="fas fa-info-circle me-2"></i>
                    密码修改功能正在开发中，敬请期待！
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
$(document).ready(function() {
    // 头像预览
    $('#id_avatar').on('change', function() {
        var file = this.files[0];
        if (file) {
            var reader = new FileReader();
            reader.onload = function(e) {
                $('.avatar').attr('src', e.target.result);
            };
            reader.readAsDataURL(file);
        }
    });
    
    // 文件类型验证
    $('#id_avatar').on('change', function() {
        var file = this.files[0];
        if (file) {
            var validTypes = ['image/jpeg', 'image/png', 'image/gif'];
            if (!validTypes.includes(file.type)) {
                alert('请选择JPG、PNG或GIF格式的图片文件');
                $(this).val('');
                return;
            }
            
            // 文件大小检查（2MB限制）
            if (file.size > 2 * 1024 * 1024) {
                alert('图片文件大小不能超过2MB');
                $(this).val('');
                return;
            }
        }
    });
    
    // 表单提交确认
    $('form').on('submit', function(e) {
        var avatar = $('#id_avatar')[0].files[0];
        if (avatar) {
            if (!confirm('确定要更新头像吗？')) {
                e.preventDefault();
                return false;
            }
        }
    });
});
</script>
{% endblock %}
